<!-- 开票申请单 -->
<template>
	<view class="page">
		
		<u--form
			class="basic-style"
			labelPosition="top"
			:model="basicForm"
			ref="form1"
			labelWidth="auto"
			:labelStyle="{
				fontSize: '14px',
				color: '#B2B2B2',
				lineHight: '20px'
			}"
		>
			<view class="basic-title basic-border">基础信息</view>
			<u-form-item
				label="单据编号"
				prop="billNo"
				:borderBottom="false"
				ref="item1"
			>
				<u--input
					disabled
					placeholder="请输入单据编号"
					v-model="basicForm.billNo"
					border="surround"
				></u--input>
			</u-form-item>
			<u-form-item
				label="申请时间"
				prop="createTime"
				:borderBottom="false"
				ref="item1"
			>
				<u--input
					v-model="basicForm.createTime"
					disabled
					placeholder="请选择时间"
					border="surround"
					suffixIcon="calendar-fill"
					:suffixIconStyle="{color: '#B2B2B2'}"
				></u--input>
			</u-form-item>
			<u-form-item
				label="申请人"
				prop="proposer_dictText"
				:borderBottom="false"
				ref="item1"
			>
				<u--input
					v-model="basicForm.proposer_dictText"
					disabled
					placeholder="请选择申请人"
					border="surround"
					suffixIcon="arrow-down-fill"
					:suffixIconStyle="{color: '#B2B2B2'}"
				></u--input>
			</u-form-item>
			<u-form-item
				label="申请部门"
				prop="proposerDeptId_dictText"
				:borderBottom="false"
				ref="item1"
			>
				<u--input
					v-model="basicForm.proposerDeptId_dictText"
					disabled
					placeholder="请选择申请部门"
					border="surround"
					suffixIcon="arrow-down-fill"
					:suffixIconStyle="{color: '#B2B2B2'}"
				></u--input>
			</u-form-item>
			
			
			<!-- 描述 当lx是1时代表申请合同 等于2的时候代表变更合同申请 -->
			<u-form-item
				v-if = "basicForm.lx == 2"
				label="销售方单位"
				prop="caId_dictText"
				:borderBottom="false"
				ref="item1"
			>
				<u--input
					disabled
					placeholder="请选择销售方单位"
					v-model="basicForm.caId_dictText"
					border="surround"
					suffixIcon="arrow-down-fill"
					:suffixIconStyle="{color: '#B2B2B2'}"
				></u--input>
			</u-form-item>
			<u-form-item
				label="经营团队"
				prop="teamId_dictText"
				:borderBottom="false"
				ref="item1"
			>
				<u--input
					v-model="basicForm.teamId_dictText"
					placeholder="请选择经营团队"
					border="surround"
					disabled
					suffixIcon="arrow-down-fill"
					:suffixIconStyle="{color: '#B2B2B2'}"
				></u--input>
			</u-form-item>
			<!-- 迭代16.7隐藏主表项目承接人 -->
			<!-- <u-form-item
				label="项目承接人"
				prop="teamId_dictText"
				:borderBottom="false"
				ref="item1"
			>
				<u--input
					v-model="basicForm.undertaker_dictText"
					placeholder="请选择项目承接人"
					border="surround"
					disabled
					suffixIcon="arrow-down-fill"
					:suffixIconStyle="{color: '#B2B2B2'}"
				></u--input>
			</u-form-item> -->
			<u-form-item
				
				label="开票人"
				prop="invoiceApplicant_dictText"
				:borderBottom="false"
				ref="item1"
				required
			>
				<u--input
					v-model="basicForm.invoiceApplicant_dictText"
					disabled
					placeholder="请选择合同类型"
					border="surround"
					suffixIcon="arrow-down-fill"
					:suffixIconStyle="{color: '#B2B2B2'}"
				></u--input>
			</u-form-item>
			<u-form-item
				label="开票类型"
				prop="invoiceCategory_dictText"
				:borderBottom="false"
				ref="item1"
				required
			>
				<u--input
					v-model="basicForm.invoiceCategory_dictText"
					disabled
					placeholder="请选择开票类型"
					border="surround"
					suffixIcon="arrow-down-fill"
					:suffixIconStyle="{color: '#B2B2B2'}"
				></u--input>
			</u-form-item>
			<!-- 迭代16.7 主表中字段是否跨月作废改为是否作废 -->
			<u-form-item
				label="是否作废"
				prop="isCrossMonthInvalid"
				:borderBottom="false"
				ref="item1"
			>
				<h-dict-radio v-model="basicForm.isCrossMonthInvalid" disabled dictName="YES_OR_NO"></h-dict-radio>
			</u-form-item>
			<u-form-item
				label="总开票金额(元)"
				prop="invoiceTotalAmount"
				:borderBottom="false"
				ref="item1"
				required
			>
				<u--input
					v-model="basicForm.invoiceTotalAmount"
					disabled
					placeholder="请输入总开票金额"
					border="surround"
				></u--input>
			</u-form-item>
			<u-form-item
				label="关联已开票申请单"
				prop="relBillId_dictText"
				:borderBottom="false"
				ref="item1"
				required
			>
				<u--input
					v-model="basicForm.relBillId_dictText"
					disabled
					placeholder="请选择已开票申请单"
					border="surround"
				></u--input>
			</u-form-item>
		</u--form>
		
		<view class="basic-style">
			<view class="basic-title basic-border">
				发票信息
			</view>
			<span style="color: red;font-size: 12px;font-weight: 400;">作废时开票金额请填负数！</span>
			<view class="inforamtion" v-for="(item, index) in getManagerClassList" :key="index">
				<view class="option">
					<view class="lf">项目名称</view>
					<view class="rl">{{ item.projectId_dictText }}</view>
				</view>
				<view class="option">
					<view class="lf">项目编号</view>
					<view class="rl">{{ item.projectCode }}</view>
				</view>
				<view class="option">
					<view class="lf">业绩归属年度</view>
					<view class="rl">{{ item.attributionYear }}</view>
				</view>
				<view class="option">
					<view class="lf">项目承接人</view>
					<view class="rl">{{ item.undertaker_dictText }}</view>
				</view>
				<view class="option">
					<view class="lf">经营团队</view>
					<view class="rl">{{ item.teamId_dictText }}</view>
				</view>
				<view class="option">
					<view class="lf">项目合同</view>
					<view class="rl">{{ item.projectContract_dictText }}</view>
				</view>
				<view class="option">
					<view class="lf">合同款项</view>
					<view class="rl">{{ item.contractFund_dictText }}</view>
				</view>
				<view class="option">
					<view class="lf">款项金额(元)</view>
					<view class="rl">{{ item.itemMoney }}</view>
				</view>
				<!-- <view class="option">
					<view class="lf">项目进度</view>
					<view class="rl">{{ item.projectProgress_dictText }}</view>
				</view> -->
				<view class="option">
					<view class="lf">预计回款时间</view>
					<view class="rl">{{ item.estimateRefundTime }}</view>
				</view>
				<view class="option">
					<view class="lf">开票金额(元)</view>
					<view class="rl">{{ item.invoiceAmount }}</view>
				</view>
			</view>
		</view>
		<view class="basic-style">
			<view class="basic-title basic-border">税率信息</view>
			<view v-for="(item, index) in taxRateData" :key="index">
				<RateFormInfo
					:isCancel="basicForm.isCrossMonthInvalid"
					:item="item"
					:index="index"
					@edit="handleEditFormRate"
					@remove="handleRemoveFormRate"
					@copy="handleCopyFormRate"
					:disabled="false"
				/>
			</view>
			<u-popup mode="bottom" :show="showFormModalRate" :closeOnClickOverlay="false">
				<RateForm
					ref="dataFormRate"
					:isCancel="basicForm.isCrossMonthInvalid"
					:index="index"
					@submit="handleFormSubmitRate"
					@close="closeFormModalRate" />
			</u-popup>
		</view>
		<view class="basic-style">
			<u--form
				labelPosition="top"
				:model="basicForm"
				ref="form1"
				labelWidth="auto"
				:labelStyle="{
					fontSize: '14px',
					color: '#B2B2B2',
					lineHight: '20px'
				}"
			>
				<u-form-item
					label="业主单位"
					prop="buyerUnit_dictText"
					:borderBottom="false"
					ref="item1"
					required
				>
					<u--input
						v-model="basicForm.buyerUnit_dictText"
						disabled
						placeholder="请选择业主单位"
						border="surround"
						suffixIcon="arrow-down-fill"
						:suffixIconStyle="{color: '#B2B2B2'}"
					></u--input>
				</u-form-item>
				<u-form-item
					label="纳税人识别号"
					prop="taxpayerIdNo"
					:borderBottom="false"
					ref="item1"
					required
				>
					<u--input
						v-model="basicForm.taxpayerIdNo"
						disabled
						placeholder="请输入纳税人识别号"
						border="surround"
					></u--input>
				</u-form-item>
				<p style="color: red;font-size: 20rpx;">如果没有税号请填写无</p>
				<u-form-item
					label="单位地址"
					prop="companyAddress"
					:borderBottom="false"
					ref="item1"
					required
				>
					<u--input
						v-model="basicForm.companyAddress"
						disabled
						placeholder="请输入单位地址"
						border="surround"
					></u--input>
				</u-form-item>
				<u-form-item
					label="单位电话"
					prop="companyTel"
					:borderBottom="false"
					ref="item1"
				>
					<u--input
						v-model="basicForm.companyTel"
						disabled
						placeholder="请输入单位电话"
						border="surround"
					></u--input>
				</u-form-item>
				<u-form-item
					label="开户行"
					prop="bankName"
					:borderBottom="false"
					ref="item1"
					required
				>
					<u--input
						v-model="basicForm.bankName"
						disabled
						placeholder="请输入开户行"
						border="surround"
					></u--input>
				</u-form-item>
				<u-form-item
					label="银行账号"
					prop="bankAccountNo"
					:borderBottom="false"
					ref="item1"
					required
				>
					<u--input
						v-model="basicForm.bankAccountNo"
						disabled
						placeholder="请输入银行账号"
						border="surround"
					></u--input>
				</u-form-item>
				<u-form-item
					label="发票备注"
					prop="invoiceRemark"
					:borderBottom="false"
					ref="item1"
					required
				>
					<u--input
						v-model="basicForm.invoiceRemark"
						disabled
						placeholder="请输入发票备注"
						border="surround"
					></u--input>
				</u-form-item>
				<u-form-item
					label="附言"
					prop="postscript"
					:borderBottom="false"
					ref="item1"
				>
					<u--input
						v-model="basicForm.postscript"
						disabled
						placeholder="请输入附言"
						border="surround"
					></u--input>
				</u-form-item>
			</u--form>
		</view>
		<view class="basic-style">
			<view class="basic-title basic-border">开票信息</view>
			<view  style="margin: 20rpx 20rpx 0rpx 20rpx;">
				<u-button text="新增" type="primary" @click="onManagerTrusteeAdd"></u-button>
			</view>
			<view class="inforamtion" style="display: flex;" v-for="(item, index) in getManagerPlanList" :key="index">
				<view >
					<view class="option" style="width: 520rpx;">
						<view class="lf">发票号码</view>
						<view class="rl">{{ item.invoiceNo }}</view>
					</view>
					<view class="option" style="width: 520rpx;">
						<view class="lf">开票日期</view>
						<view class="rl">{{ item.createTime }}</view>
					</view>
					<view class="option" style="width: 520rpx;">
						<view class="lf">发票类型</view>
						<view class="rl">{{ filterMultiDictText(dictOptions['invoiceCategory'], item.invoiceCategory + '') }}</view>
					</view>
					<view class="option" style="width: 520rpx;">
						<view class="lf">税率</view>
						<view class="rl">{{ filterMultiDictText(dictOptions['contractRateType'], item.taxRate + '') }}</view>
					</view>
					<view class="option" style="width: 520rpx;">
						<view class="lf">开票金额(元)</view>
						<view class="rl">{{ item.invoiceAmount }}</view>
					</view>
					<!-- <view class="option" style="width: 520rpx;">
						<view class="lf">是否收票</view>
						<h-dictCode class="rl" :value="item.isFree" dictName="yes_no"></h-dictCode>
					</view> -->
					<view class="option" style="width: 520rpx;">
						<view class="lf">备注</view>
						<view class="rl">{{ item.remark }}</view>
					</view>
					<view class="option" style="width: 520rpx;">
						<view class="lf">发票附件</view>
						<view class="rl">
							<file-list class="fileName" v-if="item.attachments" v-model="item.attachments"></file-list>
						</view>
					</view>
				</view>
				<view style="flex: 1; display: flex; align-items: center; justify-content: flex-end;">
					<u-icon name="edit-pen-fill" color="#2979ff" size="20" @click="onManagerTrusteeEdit(item, index)"></u-icon>
					<u-line  direction="col" length="20" margin="0 10rpx"></u-line>
					<u-icon  name="trash-fill" color="#2979ff" size="20"  @click="onManagerTrusteeDelete(item, index)"></u-icon>
				</view>
			</view>
		</view>
		
		<!-- 附件 -->
		<view class="basic-style">
			<view class="basic-title basic-border">附件信息</view>
			<!-- <file-list :fileList="basicForm.attachment" style="padding-top:16rpx"></file-list> -->
			<view class="" style="width: 100%;">
				<!-- <h-upload-file v-model="basicForm.attachments" :isIcon="false"></h-upload-file> -->
				<file-list v-model="basicForm.attachments"></file-list>
			</view>
		</view>
		
		<!-- <view class="" style="padding: 20rpx; display: flex;">
			<u-button style="margin-right:20rpx;" type="primary" text="提交" @click="onSubmit"></u-button>
			<u-button type="primary" text="审核" @click="onAudit"></u-button>
		</view> -->
		
		<view class="space-x-1 px-1 btn_position">
			<backButton  @onSubmit="onSubmit" :nodeObj="nodeObj" :model="model" :resultObj="resultObj"></backButton>
		</view>
		<view style="height: 100rpx;"></view>
		
		<!-- 开票信息弹窗 -->
		<u-popup :show="showManagerTrusteePopup" @close="closeManagerTrusteePopup" :round="10">
			<view style="background-color: #ffffff; border-radius: 10px; padding: 20rpx;">
				<view style="max-height: 80vh; overflow-y: auto;padding: 10px;">
					<u--form
						labelPosition="top"
						:model="managerTrusteeForm"
						ref="managerTrusteeForm1"
						labelWidth="auto"
						:labelStyle="{
							fontSize: '14px',
							color: '#B2B2B2',
							lineHight: '20px'
						}">
						<u-form-item
							label="发票号码"
							prop="invoiceNo"
							:borderBottom="false"
							ref="item1"
							required
						>
							<u--input
								v-model="managerTrusteeForm.invoiceNo"
								placeholder="请输入发票号码"
								border="surround"
							></u--input>
						</u-form-item>
						<u-form-item
							label="开票日期"
							prop="createTime"
							:borderBottom="false"
							ref="item1"
							required
						>
							<h-time-select
								v-model="managerTrusteeForm.createTime"
								placeholder="请选择开票日期"
								border="surround"
								suffixIcon="calendar-fill"
								:suffixIconStyle="{color: '#B2B2B2'}"
								mode="datetime"
							></h-time-select>
						</u-form-item>
						<u-form-item
							label="发票类型"
							prop="invoiceCategory"
							:borderBottom="false"
							ref="item1"
							required>
							<h-dict-select
								style="width: 100%" 
								v-model="managerTrusteeForm.invoiceCategory" 
								placeholder="请选择发票类型" 
								dictName="invoice_type" 
								suffixIcon="arrow-down-fill" 
								:suffixIconStyle="{color: '#B2B2B2'}"
							></h-dict-select>
						</u-form-item>
						<u-form-item
							label="税率"
							prop="taxRate"
							:borderBottom="false"
							required
							ref="item1">
							<h-dict-select
								style="width: 100%" 
								v-model="managerTrusteeForm.taxRate" 
								placeholder="请选择税率" 
								dictName="contract_rate_type"
							></h-dict-select>
						</u-form-item>
						<u-form-item label="开票金额(元)" prop="invoiceAmount" :required="true">
							<h-money-input
								placeholder="请输入开票金额"
								:isMinus="false"
								v-model="managerTrusteeForm.invoiceAmount"
								:max="99999999"
								:min="-99999999"
							></h-money-input>
						</u-form-item>
						<!-- <u-form-item
							label="是否收票"
							prop="isFree"
							:borderBottom="false"
							ref="item1"
							required
						>
							<h-dict-radio v-model="managerTrusteeForm.isFree" dictName="YES_OR_NO"></h-dict-radio>
						</u-form-item> -->
						<u-form-item
							label="备注"
							prop="remark"
							:borderBottom="false"
							ref="item1">
							<u--textarea confirmType="done" v-model="managerTrusteeForm.remark" placeholder="请输入备注" ></u--textarea>
						</u-form-item>
						<u-form-item label="发票附件" prop="attachments" required>
							<view class="space-y-1" style="width: 100%;">
								<view style="color: red">提示：请等待附件上传完成，再点击保存，否则附件无法保存成功！</view>
								<h-upload-file v-model="managerTrusteeForm.attachments" :isIcon="false"></h-upload-file>
								<file-list v-if="managerTrusteeForm.attachments" v-model="managerTrusteeForm.attachments" :isDelete="true"></file-list>
							</view>
						</u-form-item>
					</u--form>
				</view>
				<view style="padding-bottom:20rpx;">
					<u-button text="确定" type="primary" @click="onConfirmManagerTrusteePopup"></u-button>
				</view>
			</view>
		</u-popup>
		
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
import fileList from './components/fileList.vue'
import backButton from '@/components/backButton.vue'
import RateForm from '@/pages/finance/components/RateForm.vue' // 税率表单
import RateFormInfo from '@/pages/finance/components/RateFormInfo.vue' // 税率列表
import { gainBillNo, add, info,edit,queryEmpIdByCompanyIdAndPostCode,queryInvoiceExpensesDetailByBillId, erpFinanceInvoiceTaxRateRecordList, queryContractRateFinal, queryContractPaymentNodeFinal } from '@/api/finance/ErpFinanceInvoiceBillForm'
import {
	queryByIdInvoice, // 根据id查询信息
	queryByIdInvoiceChildren, // 开票费用信息
	getProcessTaskTransInfo, // 流程流转
	erpFinanceInvoiceHandleRecordList, // 开票信息列表
	addInvoiceHandleRecordList ,// 编辑
	
} from '@/api/index.js'
import {
	BASE_URL
} from '@/utils/baseUrl.js'
import { filterMultiDictText, initDictOptions } from '@/utils/JDictSelectUtil.js'

export default {
	components: {
		fileList,
		backButton,
		RateForm,
		RateFormInfo
	},
	data() {
		return {
			resultObj: {}, // 流程图流转信息
			filterMultiDictText,
			initDictOptions,
			dictOptions: {},
			
			isSubmit: false, // 是否点击提交
			nodeObj: {},
			tabvalue:'',
			dataNodeObj: '',
			id: '', // 详情id值
			basicForm: {},
			model: {},
			getManagerClassList: [], // 开票费用信息列表
			getManagerPlanList: [], // 开票信息列表
			taxRateData: [], // 税率信息
			showManagerTrusteePopup: false, // 受托方弹窗
			showFormModalRate: false, // 税率信息弹窗
			clickManagerTrusteePopup: '', // 点击编辑还是新增
			managerTrusteeForm: {
				invoiceNo: '',
				createTime: '',
				invoiceCategory: '0',
				taxRate: '',				invoiceAmount: '',
				attachments: '',
				// isFree: '',
				remark: '',
				billId: ''
			}, // 对象
			managerTrusteeIndex: 0, // 点击编辑的index值
			index: null,
		};
	},
	onLoad(options) {
		var that = this
		this.initDictConfig()
		this.model = JSON.parse(decodeURIComponent(options.item))
		var eventChannel
		// #ifdef APP-NVUE
			eventChannel = this.$scope.eventChannel; // 兼容APP-NVUE
		// #endif
		// #ifndef APP-NVUE
			eventChannel = this.getOpenerEventChannel();
		// #endif
		// 监听sendMessage事件，获取上一页面通过eventChannel传送到当前页面的数据
		eventChannel.on('sendMessage', async function(data) {
			that.tabvalue = data.tabvalue
			that.nodeObj =  await JSON.parse(decodeURIComponent(data.nodeObj))
			that.dataNodeObj = data.nodeObj
			that.loadData(that.nodeObj.dataId)
			that.id = that.nodeObj.dataId
			that.getErpOmcContractManagerClassList() // 开票费用信息列表
			that.getErpOmcContractManagerPlanList() // 开票信息列表
			that.getRateList() // 税率信息列表
			that.getProcessTaskTrans(that.model.id) // 流程流转
		})
	},
	methods: {
		loadData(id){
			queryByIdInvoice({ id }).then(res => {
				if(res.success){
					this.basicForm = res.result
				} else {
					this.$refs.uToast.show({
						message: res.message,
						type: 'error'
					})
				}
			})
		},
		
		async initDictConfig() {
			initDictOptions('invoice_type').then(async(res) => {
				if (res.success) {
					this.$set(this.dictOptions, 'invoiceCategory', res.result)
				}
			})
			// 税率
			initDictOptions('contract_rate_type').then(async(res) => {
				if (res.success) {
					this.$set(this.dictOptions, 'contractRateType', res.result)
				}
			})
		},
		// 获取开票费用信息
		getErpOmcContractManagerClassList () {
			var params = {
				column: 'createTime',
				order: 'desc',
				billId: this.id
			}
			queryByIdInvoiceChildren (params).then(res => {
				if (res.success) {
					this.getManagerClassList = res.result || []
				} else {
					this.$refs.uToast.show({
						message: res.message,
						type: 'error'
					})
				}
			})
		},
		// 获取开票信息
		getErpOmcContractManagerPlanList () {
			var params = {
				column: 'createTime',
				order: 'desc',
				pageNo: 1,
				pageSize: 100,
				billId: this.id
			}
			erpFinanceInvoiceHandleRecordList (params).then(res => {
				if (res.success) {
					if (res.result.records && res.result.records.length) {
						res.result.records.map(value => {
							value.infoDisabled = true
							return value
						})
					}
					this.getManagerPlanList = res.result.records || []
				} else {
					this.$refs.uToast.show({
						message: res.message,
						type: 'error'
					})
				}
			})
		},
		async getRateList () {
			// 获取税率信息
			const rateList = await erpFinanceInvoiceTaxRateRecordList({billId: this.id,pageNo: 1, pageSize: 100 })
			this.taxRateData = rateList.result.records || rateList.result || []
		},
		// 点击新增委托方弹窗
		onManagerTrusteeAdd () {
			this.managerTrusteeForm = {
				invoiceNo: '',
				createTime: '',
				invoiceCategory: '0',
				taxRate: '',
				invoiceAmount: '',
				attachments: '',
				// isFree: '',
				remark: '',
				billId: ''
			} // 清空数据
			this.clickManagerTrusteePopup = 'add'
			this.showManagerTrusteePopup = true
		},
		
		// 点击编辑委托方弹窗
		onManagerTrusteeEdit (item, index) {
			this.managerTrusteeForm.invoiceNo = item.invoiceNo
			this.managerTrusteeForm.createTime = item.createTime
			this.managerTrusteeForm.invoiceCategory = item.invoiceCategory
			this.managerTrusteeForm.taxRate = item.taxRate
			this.managerTrusteeForm.invoiceAmount = item.invoiceAmount
			this.managerTrusteeForm.attachments = item.attachments
			// this.managerTrusteeForm.isFree = item.isFree
			this.managerTrusteeForm.remark = item.remark
			this.managerTrusteeForm.billId = item.billId
			this.clickManagerTrusteePopup = 'edit'
			this.managerTrusteeIndex = index
			this.showManagerTrusteePopup = true
		},
		
		// 点击删除委托方弹窗
		onManagerTrusteeDelete (item, index) {
			let _this = this
			uni.showModal({
				title: '确定删除',
				content: '确定删除此信息？',
				success (res) {
						if (res.confirm) {
							_this.getManagerPlanList.splice(index, 1)
						} else if (res.cancel) {
							// 用户点击取消
						}
					}
				
			})
		},
		
		// 委托方确定按钮
		onConfirmManagerTrusteePopup () {
			if (!this.managerTrusteeForm.invoiceNo) {
				uni.showToast({
					title: '请输入发票号码',
					icon: 'none'
				})
				return false
			}
			if (!this.managerTrusteeForm.createTime) {
				uni.showToast({
					title: '请选择开票日期',
					icon: 'none'
				})
				return false
			}
			if (!this.managerTrusteeForm.invoiceCategory) {
				uni.showToast({
					title: '请选择发票类型',
					icon: 'none'
				})
				return false
			}
			if (!this.managerTrusteeForm.taxRate) {
				uni.showToast({
					title: '请选择税率',
					icon: 'none'
				})
				return false
			}
			if (this.managerTrusteeForm.invoiceAmount === null || this.managerTrusteeForm.invoiceAmount === undefined || this.managerTrusteeForm.invoiceAmount === '') {
				uni.showToast({
					title: '请输入开票金额',
					icon: 'none'
				})
				return false
			}
			if (!this.managerTrusteeForm.attachments) {
				uni.showToast({
					title: '请上传发票附件',
					icon: 'none'
				})
				return false
			}
			// if (this.managerTrusteeForm.isFree == '') {
			// 	uni.showToast({
			// 		title: '请选择是否收票',
			// 		icon: 'none'
			// 	})
			// 	return false
			// }
			// this.getManagerTrusteeId(this.managerTrusteeForm.gsdaId)
			
			// var idPar = parseInt(Math.random() * 1000000000000);
			if (this.clickManagerTrusteePopup == 'edit') {
				this.getManagerPlanList[this.managerTrusteeIndex].invoiceNo = this.managerTrusteeForm.invoiceNo
				this.getManagerPlanList[this.managerTrusteeIndex].createTime = this.managerTrusteeForm.createTime
				this.getManagerPlanList[this.managerTrusteeIndex].invoiceCategory = this.managerTrusteeForm.invoiceCategory
				this.getManagerPlanList[this.managerTrusteeIndex].taxRate = this.managerTrusteeForm.taxRate
				this.getManagerPlanList[this.managerTrusteeIndex].invoiceAmount = this.managerTrusteeForm.invoiceAmount
				this.getManagerPlanList[this.managerTrusteeIndex].attachments = this.managerTrusteeForm.attachments
				// this.getManagerPlanList[this.managerTrusteeIndex].isFree = this.managerTrusteeForm.isFree
				this.getManagerPlanList[this.managerTrusteeIndex].remark = this.managerTrusteeForm.remark
				this.getManagerPlanList[this.managerTrusteeIndex].billId = this.managerTrusteeForm.billId
				// this.getManagerPlanList.map((value,index) => {
				// 	if (index == this.managerTrusteeIndex) {
				// 		value = {...this.managerTrusteeForm}
				// 		return value
				// 	}
				// })
			} else {
				this.managerTrusteeForm.billId = this.id
				this.getManagerPlanList.push(this.managerTrusteeForm)
			}
			this.showManagerTrusteePopup = false
			this.managerTrusteeForm = {
				invoiceNo: '',
				createTime: '',
				invoiceCategory: '',
				taxRate: '',
				invoiceAmount: '',
				attachments: '',
				// isFree: '',
				remark: '',
				billId: ''
			} // 清空数据
		},
		
		// 关闭委托方弹窗
		closeManagerTrusteePopup () {
			this.showManagerTrusteePopup = false
		},
		/* 税率信息 start */
		handleEditFormRate([item, index]) {
			if (!this.basicForm.isCrossMonthInvalid) {
				uni.showToast({
				  title: '请选择是否作废!',
				  icon: 'none',
				})
				return false
			}
		  this.basicFormRate = { ...item }
		  this.index = index
		  this.showFormModalRate = true
			this.$nextTick(() => {
				this.openFormModalRate()
			})
		},
		handleRemoveFormRate(item) {
		  this.taxRateData = this.taxRateData.filter((i) => i !== item)
		},
		closeFormModalRate() {
		  this.$refs.dataFormRate.reset()
		  this.basicFormRate = {}
		  this.index = null
			this.showFormModalRate = false
		},
		openFormModalRate() {
		  this.$refs.dataFormRate.basicForm = Object.assign({},this.basicFormRate)
		},
		handleCopyFormRate([item, index]) {
		  this.taxRateData.push({
		    ...item,
		  })
		  this.$refs.uToast.show({
		  	message:'复制成功!'
		  })
		},
		addFormRate() {
			if (!this.basicForm.isCrossMonthInvalid) {
				uni.showToast({
				  title: '请选择是否作废!',
				  icon: 'none',
				})
				return false
			}
		  this.index = null
			this.basicFormRate = {}
		  this.showFormModalRate = true
		},
		/**
		 * 子表单新增点击确认
		 */
		handleFormSubmitRate([data, index]) {
		  this.showFormModalRate = false
		  if (typeof index === 'number') {
		    this.$set(this.taxRateData, index, data)
		  } else {
		    this.taxRateData.push(data)
		  }
		},
		/* 税率信息 end */
		// 查询流程流转信息 -- 获取下一节点信息
		getProcessTaskTrans(taskId){
			getProcessTaskTransInfo({ taskId }).then(res => {
				if(res.success){
					this.resultObj = res.result
				} else {
					this.$refs.uToast.show({
						message: res.message,
						type: 'error'
					})
				}
			}).catch(err => {
				console.log(err);
			})
		},
		judgeInvoiceHandleList() {
			const groupedData = this.getManagerClassList.reduce((acc, item) => {
				if (!acc[item.contractFund]) {
					acc[item.contractFund] = [];
				}
				acc[item.contractFund].push(item);
				return acc;
			}, {});
			for (const key in groupedData) {
				if(groupedData[key].length>1){
					return true;
				}
			}
			return false;
		},
		judgeTaxRateHandle() {
			let count = 0
			this.taxRateData.forEach((item, index) => {
				count = count + Number(item.invoiceAmount || 0)
			})
			if(this.basicForm.invoiceTotalAmount!=count){
				return true;
			}
			return false;
		},
		// 点击提交
		onSubmit () {
			var _this = this
			if (!this.getManagerPlanList || !this.getManagerPlanList.length){
				uni.showToast({
					title: '必须填写至少一条开票信息!',
					icon: 'none'
				})
				return false
			}
			if(this.judgeInvoiceHandleList()){
				uni.showToast({
					title: '同一个款项不能重复开票!',
					icon: 'none'
				})
				return false
			}
			if(this.judgeTaxRateHandle()){
				uni.showToast({
					title: '税率信息的本次开票金额总和必须等于总开票金额!',
					icon: 'none'
				})
				return false
			}
			let kp = this.addInvoiceHandle()
			if (!kp) {
				return false;
			}
			var params = {
				...this.basicForm,
				invoiceExpensesDetails: this.getManagerClassList,
				invoiceTaxRateDetails: this.taxRateData
			}
			edit(params).then(res => {
				if (res.success) {
					this.$refs.uToast.show({
						message: '提交成功',
						type: 'success',
						complete () {
							_this.onAudit()
						}
					})
					this.isSubmit = true // 是否提交
				} else {
					this.$refs.uToast.show({
						message: res.message,
						type: 'error'
					})
				}
			})
		},
		async addInvoiceHandle() {
			let isOk = false;
			var params = this.getManagerPlanList
			await addInvoiceHandleRecordList(this.id, params).then((res) => {
				if(res.success){
					isOk = true;
				}else{
					this.$refs.uToast.show({
						message: res.message,
						type: 'error'
					})
				}
			})
			return isOk;
		},
		
		onAudit() {
			if (!this.isSubmit) {
				uni.showToast({
					title: '请完善表单后审核',
					icon: 'none'
				})
				return false
			}
			var that = this
			uni.navigateTo({
				url: '/pages/synergy/invoiceApplicationBill'+`?item=${encodeURIComponent(JSON.stringify(this.model).replace(/%/g, '%25'))}`,
				events: {
					// 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
					// acceptDataFromOpenedPage: function(data) {
					// }
				},
				success: function(data) {
					// 通过eventChannel向被打开页面传送数据
					var params = {
						nodeObj: encodeURIComponent(JSON.stringify(that.nodeObj).replace(/%/g, '%25')),
						tabvalue: that.tabvalue
					}
					data.eventChannel.emit('sendMessage', params)
				},
				fail: function(err) {
					this.$refs.uToast.show({
						message: '请联系管理员配置',
						type: 'error'
					})
				}
			})
		}
		
	}
};
</script>

<style lang="scss" scoped>
@import url("@/assets/inputStyle.scss");
// 评测信息
.info-title{
	font-size: 28rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #171717;
	line-height: 40rpx;
	padding-bottom: 16rpx;
}

// 评测结果
.result-style{
	font-size: 24rpx;
	font-family: PingFangSC-Regular, PingFang SC;
	font-weight: 400;
	color: #B2B2B2;
}

.inforamtion{
	margin-top: 10rpx;
	padding: 10rpx;
	border-radius: 15rpx;
	box-shadow: 0px 4px 20px 0px rgba(0,0,0,0.1);
}
.fileName {
	padding: 0 !important;
}
/deep/ .fileName .file_name {
	font-size: 14px;
	font-weight: 400;
	color: #7f7f7f;
	// line-height: 14px;
	overflow: visible !important;
	white-space: normal !important;
	text-overflow: ellipsis;
	max-width: 460rpx;
}
/deep/ .fileName .file {
	padding: 0px !important;
}
</style>